package com.xquant.common.engine.xPP.request.instrument;

import com.xquant.common.engine.utils.DateUtils;
import com.xquant.common.engine.xPP.request.Instrument;
import com.xquant.common.engine.xPP.request.scene.Factor;

/**
 * @author B946
 * @date 28/09/2020 09:59
 * @description:
 */
public abstract class AbstractProduct implements IProduct {


    /**
     * 根据情景因子构建金融工具
     * @param instrument
     * @param factor
     * @return
     */
    @Override
    public Instrument buildInstrument(Instrument instrument, Factor factor){
        if(Factor.FACTOR_TERM.equals(factor.getFactor())){
            return replaceTerm(instrument, factor.getType(), factor.getValue());
        }else if(Factor.FACTOR_STRIKE.equals(factor.getFactor())){
            return replaceStrike(instrument, factor.getType(), factor.getValue());
        }else if(Factor.FACTOR_STRIKE_PERCENTAGE.equals(factor.getFactor())){
            return replaceStrikePercentage(instrument, factor.getType(), factor.getValue());
        }
        return instrument;
    }
    /**
     * 期限只支持值类型为值的，不支持偏移。
     * @param instrument
     * @param valueType
     * @param term
     * @return
     */
    protected Instrument replaceTerminationDate(Instrument instrument,String valueType,String term){
        if(!Factor.VALUE_TYPE_VALUE.equals(valueType)){
            return instrument;
        }
        String effectiveDate = instrument.getTermsheet().getString("effectiveDate");
        instrument.getTermsheet().put("terminationDate",replaceDate(effectiveDate,term));
        return instrument;
    }

    /**
     * 用空替换无法确定，或者难以修改的日期
     * @param instrument
     * @return
     */
    protected Instrument replaceDateByEmpty(Instrument instrument){

        return instrument;
    }


    /**
     * 获取新的日期，只支持term格式为*D,*M,*W,*Y的期限
     * @param date
     * @param term
     * @return
     */
    protected String replaceDate(String date,String term){
        String newDate = DateUtils.addTerm(date,term);
        return newDate == null ? date : newDate;
    }

    /**
     * 根据报价因子替换行权价
     * @param instrument
     * @param valueType
     * @param strike
     * @return
     */
    protected Instrument replaceStrike(Instrument instrument,String valueType,String strike){
        return instrument;
    }
    /**
     * 根据报价因子替换行权价比例
     * @param instrument
     * @param valueType
     * @param strike
     * @return
     */
    protected Instrument replaceStrikePercentage(Instrument instrument,String valueType,String strikePercentage){
        return instrument;
    }
    /**
     * 根据报价因子替换行期限
     * @param instrument
     * @param valueType
     * @param strike
     * @return
     */
    protected Instrument replaceTerm(Instrument instrument,String valueType,String term){
        replaceTerminationDate(instrument, valueType, term);
        return instrument;
    }

}
